home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d16 / winsrc.arc / WINFRACT.C < prev    next >
C/C++ Source or Header  |  1991-06-16  |  56KB  |  1,543 lines

  1. /****************************************************************************
  2.  
  3.  
  4.     PROGRAM: winfract.c
  5.  
  6.     PURPOSE: Windows-specific main-driver code for Fractint for Windows
  7.              (look in MAINFRAC.C for the non-windows-specific code)
  8.  
  9.     Copyright (C) 1990 The Stone Soup Group.  Fractint for Windows 
  10.     may be freely copied and distributed, but may not be sold.
  11.     
  12.     We are, of course, copyrighting the code we wrote to implement
  13.     Fractint-for-Windows, and not the routines we lifted directly
  14.     or indirectly from Microsoft's Windows 3.0 Software Development Kit.
  15.  
  16. ****************************************************************************/
  17.  
  18. #include "windows.h"
  19. #include "winfract.h"
  20. #include "mathtool.h"
  21. #include "fractype.h"
  22. #include "fractint.h"
  23. #include "select.h"
  24. #include <math.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <search.h>
  28. #include <string.h>
  29. #include <time.h>
  30. #include "profile.h"
  31.  
  32. extern LPSTR win_lpCmdLine;
  33.  
  34. HANDLE hInst;
  35.  
  36. HWND hwnd;                               /* handle to main window */
  37. HWND hWndCopy;                 /* Copy of hWnd */
  38.  
  39. #define PALETTESIZE 256               /* dull-normal VGA                    */
  40. HANDLE hpal;                          /* palette handle                     */
  41. PAINTSTRUCT ps;                       /* paint structure                    */
  42. HDC hDC;                              /* handle to device context           */
  43. HDC hMemoryDC;                        /* handle to memory device context    */
  44. BITMAP Bitmap;                        /* bitmap structure                   */
  45. unsigned IconWidth, IconHeight, IconSize;
  46. HANDLE hIconBitmap;
  47.  
  48. HANDLE  hPal;          /* Handle to the application's logical palette  */
  49. HANDLE  hLogPal;       /* Temporary Handle */
  50. LPLOGPALETTE pLogPal;  /* pointer to tha application's logical palette */
  51. int     iNumColors;    /* Number of colors supported by device           */
  52. int     iRasterCaps;   /* Raster capabilities                           */
  53. int     iPalSize;      /* Size of Physical palette                       */
  54.  
  55. BOOL    win_systempaletteused = FALSE;    /* flag system palette set */
  56. extern int win_syscolorindex[21];
  57. extern DWORD win_syscolorold[21];
  58. extern DWORD win_syscolornew[21];
  59.  
  60. /* MCP 6-16-91 */
  61. extern int pixelshift_per_byte;
  62. extern long pixels_per_bytem1;        /* pixels / byte - 1 (for ANDing) */
  63. extern unsigned char win_andmask[8];
  64. extern unsigned char win_notmask[8];
  65. extern unsigned char win_bitshift[8];
  66.  
  67. extern int CoordBoxOpen;
  68. extern HWND hCoordBox;
  69. extern int TrackingZoom, Zooming, ReSizing;
  70.  
  71. #define EXE_NAME_MAX_SIZE  128
  72.  
  73. BOOL       bHelp = FALSE;      /* Help mode flag; TRUE = "ON"*/
  74. HCURSOR    hHelpCursor;        /* Cursor displayed when in help mode*/
  75. char       szHelpFileName[EXE_NAME_MAX_SIZE+1];    /* Help file name*/
  76.  
  77. void MakeHelpPathName(char*);  /* Function deriving help file path */
  78.  
  79. unsigned char far win_dacbox[256][3];
  80. extern unsigned char dacbox[256][3];
  81. int max_colors;
  82.  
  83. BOOL bTrack = FALSE;                  /* TRUE if user is selecting a region */
  84. BOOL zoomflag = FALSE;                /* TRUE is a zoom-box selected */
  85. RECT Rect;
  86.  
  87. int Shape = SL_BLOCK;            /* shape to use for the selection rectangle */
  88.  
  89. /* pointers to various dialog-box routines */
  90. FARPROC lpProcAbout;
  91. FARPROC lpOpenDlg;
  92. FARPROC lpSelectFractal;
  93. FARPROC lpSelectFracParams;
  94. FARPROC lpSelectImage;
  95. FARPROC lpSelectDoodads;
  96. FARPROC lpSelectCycle;
  97. FARPROC lpSaveAsDlg;
  98. FARPROC lpProcStatus;
  99. FARPROC lpSelect3D;
  100. FARPROC lpSelect3DPlanar;
  101. FARPROC lpSelect3DSpherical;
  102.  
  103. extern unsigned char readname[], FileName[], FormFileName[];
  104. extern unsigned char LFileName[], LName[];
  105. extern unsigned char DialogTitle[], DefSpec[], DefExt[];
  106.  
  107. HBITMAP hBitmap, oldBitmap, oldoldbitmap;              /* working bitmaps */
  108.  
  109. HANDLE hDibInfo;        /* handle to the Device-independent bitmap */
  110. LPBITMAPINFO pDibInfo;        /* pointer to the DIB info */
  111. HANDLE hpixels;            /* handle to the DIB pixels */
  112. unsigned char huge *pixels;    /* the device-independent bitmap  pixels */
  113. extern int bytes_per_pixelline;    /* pixels/line / pixels/byte */
  114. extern long win_bitmapsize;     /* size of the DIB in bytes */
  115.  
  116. HANDLE hClipboard1, hClipboard2, hClipboard3; /* handles to clipboard info */
  117. LPSTR lpClipboard1, lpClipboard2;            /* pointers to clipboard info */
  118.  
  119. int last_written_y = -2;        /* last line written */
  120. int screen_to_be_cleared = 1;    /* flag that the screen is to be cleared */
  121. int time_to_act = 0;        /* time to take some sort of action? */
  122. int time_to_restart = 0;        /* time to restart?  */
  123. int time_to_reinit = 0;         /* time to reinitialize?  */
  124. int time_to_quit = 0;           /* time to quit? */
  125. int time_to_save = 0;        /* time to save the file? */
  126. int time_to_print = 0;        /* time to print the file? */
  127. int time_to_load = 0;        /* time to load a new file? */
  128. int time_to_cycle = 0;          /* time to begin color-cycling? */
  129.  
  130. int win_3dspherical = 0;          /* spherical 3D? */
  131. int win_display3d, win_overlay3d; /* 3D flags */
  132.  
  133. extern int win_cycledir, win_cyclerand;
  134.  
  135. extern int calc_status;
  136.  
  137. int xdots, ydots, colors, maxiter;
  138. int ytop, ybottom, xleft, xright;
  139. int xposition, yposition, win_xoffset, win_yoffset, xpagesize, ypagesize;
  140. int win_xdots, win_ydots;
  141. extern int fractype;
  142. extern double param[4];
  143. extern double xxmin, xxmax, yymin, yymax, xx3rd, yy3rd;
  144. double jxxmin, jxxmax, jyymin, jyymax, jxx3rd, jyy3rd;
  145. extern int frommandel, bitshift, biomorph;
  146.  
  147. extern char str[255];
  148.  
  149. int cpu, fpu;            /* cpu, fpu flags */
  150.  
  151. extern int win_release;
  152.  
  153. char *win_choices[100];
  154. int win_numchoices, win_choicemade;
  155.  
  156. extern int onthelist[];
  157. extern int CountFractalList;
  158. extern int CurrentFractal;
  159. int MaxFormNameChoices = 80;
  160. char FormNameChoices[80][21];
  161. extern char FormName[30];
  162. extern char    IFSFileName[];    /* IFS code file */
  163. extern char    IFSName[];        /* IFS code item */
  164. double far *temp_array;
  165. HANDLE htemp_array;
  166.  
  167. HANDLE hSaveCursor;             /* the original cursor value */
  168. HANDLE hHourGlass;              /* the hourglass cursor value */
  169.  
  170. /****************************************************************************
  171.  
  172.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  173.  
  174.     PURPOSE: calls initialization function, processes message loop
  175.  
  176. ****************************************************************************/
  177.  
  178. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  179. HANDLE hInstance;
  180. HANDLE hPrevInstance;
  181. LPSTR lpCmdLine;
  182. int nCmdShow;
  183. {
  184.     int Return;
  185.  
  186.     win_lpCmdLine = lpCmdLine;
  187.  
  188.     if (!hPrevInstance)
  189.         if (!InitApplication(hInstance))
  190.             return (FALSE);
  191.  
  192.     if (!InitInstance(hInstance, nCmdShow))
  193.         return (FALSE);
  194.  
  195.     fractint_main();            /* fire up the main Fractint code */
  196.     if(htemp_array) {
  197.         GlobalUnlock(htemp_array);
  198.         GlobalFree(htemp_array);
  199.     }
  200.     DestroyWindow(hWndCopy);    /* stop everything when it returns */
  201.  
  202.     return(0);                  /* we done when 'fractint_main' returns */
  203. }
  204.  
  205. /****************************************************************************
  206.  
  207.     FUNCTION: InitApplication(HANDLE)
  208.  
  209.     PURPOSE: Initializes window data and registers window class
  210.  
  211. ****************************************************************************/
  212.  
  213. BOOL InitApplication(hInstance)
  214. HANDLE hInstance;
  215. {
  216.     WNDCLASS  wc;
  217.  
  218.     wc.style = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
  219.     wc.lpfnWndProc = MainWndProc;
  220.     wc.cbClsExtra = 0;
  221.     wc.cbWndExtra = 0;
  222.     wc.hInstance = hInstance;
  223.     wc.hIcon = NULL;
  224.     /* the line below disables our internal icon-drawing logic
  225.     wc.hIcon = LoadIcon(hInstance, "FracIcon");                */
  226.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  227.     wc.hbrBackground = GetStockObject(BLACK_BRUSH);
  228.     wc.lpszMenuName =  "WinFracMenu";
  229.     wc.lpszClassName = "FractintForWindowsV0010";
  230.  
  231.     return (RegisterClass(&wc) && RegisterMathWindows(hInstance));
  232. }
  233.  
  234.  
  235. /****************************************************************************
  236.  
  237.     FUNCTION:  InitInstance(HANDLE, int)
  238.  
  239.     PURPOSE:  Saves instance handle and creates main window
  240.  
  241. ****************************************************************************/
  242. BOOL InitInstance(hInstance, nCmdShow)
  243.     HANDLE          hInstance;
  244.     int             nCmdShow;
  245. {
  246.     DWORD WinFlags;
  247.     WORD  DeviceCaps;
  248.     int iLoop, jLoop;
  249.     DWORD ThisColor;
  250.  
  251.     float temp;
  252.     char tempname[40];
  253.  
  254.     /* so, what kind of a computer are we on, anyway? */
  255.     WinFlags = GetWinFlags();
  256.     cpu = 88;                             /* determine the CPU type */
  257.     if (WinFlags & WF_CPU186) cpu = 186;
  258.     if (WinFlags & WF_CPU286) cpu = 286;
  259.     if (WinFlags & WF_CPU386) cpu = 386;
  260.     if (WinFlags & WF_CPU486) cpu = 386;
  261.     fpu = 0;                              /* determine the FPU type */
  262.     if (WinFlags & WF_80x87)  fpu = 87;
  263.     if ((WinFlags & WF_CPU486) || (fpu && (cpu == 386))) fpu = 387;
  264.  
  265.     hInst = hInstance;
  266.  
  267.     temp = win_release / 100.0;
  268.     sprintf(tempname,"Fractint for Windows - Vers %5.2f", temp);
  269.  
  270.     hwnd = CreateWindow(
  271.         "FractintForWindowsV0010",
  272.         tempname,
  273.         WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL,
  274.         160, 120,  /* initial locn instead of CW_USEDEFAULT, CW_USEDEFAULT, */
  275.         320, 240,  /* initial size instead of CW_USEDEFAULT, CW_USEDEFAULT, */
  276.         NULL,
  277.         NULL,
  278.         hInstance,
  279.         NULL
  280.     );
  281.  
  282.     if (!hwnd) {  /* ?? can't create the initial window! */
  283.         return (FALSE);
  284.         }
  285.  
  286.     /* This program doesn't run in "real" mode, so shut down right
  287.        now to keep from mucking up Windows */
  288.     if (!((WinFlags & WF_STANDARD) || (WinFlags & WF_ENHANCED))) {
  289.         MessageBox (
  290.             GetFocus(),
  291.             "This program requires Standard\nor 386-Enhanced Mode",
  292.             "Fractint For Windows",
  293.             MB_ICONSTOP | MB_OK);
  294.         return(FALSE);
  295.         }
  296.     
  297.     win_xdots = xdots;
  298.     win_ydots = ydots;
  299.     maxiter = 150;                   /* and a few iterations */
  300.     xposition = yposition = 0;       /* dummy up a few pointers */
  301.     xpagesize = ypagesize = 2000;
  302.     set_win_offset();
  303.  
  304.     /* obtain an hourglass cursor */
  305.     hHourGlass  = LoadCursor(NULL, IDC_WAIT);
  306.     hSaveCursor = LoadCursor(NULL, IDC_ARROW);
  307.  
  308.     SizeWindow(hwnd);
  309.     ShowWindow(hwnd, nCmdShow);
  310.     UpdateWindow(hwnd);
  311.  
  312.     /* let's ensure that we have at lease 40K of free memory */
  313.     {
  314.     HANDLE temphandle;
  315.     if (!(temphandle = GlobalAlloc(GMEM_FIXED,40000L)) ||
  316.         !(htemp_array = GlobalAlloc(GMEM_FIXED, sizeof(double) * 8001))) {
  317.         MessageBox (
  318.             GetFocus(),
  319.             "There isn't enough available\nmemory to run Winfract.",
  320.             "Fractint For Windows",
  321.             MB_ICONSTOP | MB_OK);
  322.         return(FALSE);
  323.         }
  324.         GlobalLock(temphandle);
  325.         GlobalUnlock(temphandle);
  326.         GlobalFree(temphandle);
  327.         temp_array = (double far *)GlobalLock(htemp_array);
  328.     }
  329.  
  330.    MakeHelpPathName(szHelpFileName);
  331.  
  332.     /* so, what kind of a display are we using, anyway? */
  333.     hDC = GetDC(NULL);
  334.     iPalSize    = GetDeviceCaps (hDC, SIZEPALETTE);
  335.     iRasterCaps = GetDeviceCaps (hDC, RASTERCAPS);
  336.     iRasterCaps = (iRasterCaps & RC_PALETTE) ? TRUE : FALSE;
  337.     if (iRasterCaps)
  338.        iNumColors = GetDeviceCaps(hDC, SIZEPALETTE);
  339.     else
  340.        iNumColors = GetDeviceCaps(hDC, NUMCOLORS);
  341.     ReleaseDC(NULL,hDC);
  342.  
  343.     /* fudges for oddball stuff (is there any?) */
  344.     /* also, note that "true color" devices return -1 for NUMCOLORS */
  345.     colors = iNumColors;
  346.     if (colors < 2 || colors > 16) colors = 256;
  347.     if (colors > 2 && colors < 16) colors = 16;
  348.     /* adjust for Windows' 20 reserved palettes in 256-color mode */
  349.     max_colors = 256;
  350.     if (colors == 256) max_colors = 236;
  351.  
  352.      /* Allocate enough memory for a logical palette with
  353.       * PALETTESIZE entries and set the size and version fields
  354.       * of the logical palette structure.
  355.       */
  356.      if (!(hLogPal = GlobalAlloc (GMEM_FIXED,
  357.         (sizeof (LOGPALETTE) +
  358.         (sizeof (PALETTEENTRY) * (PALETTESIZE)))))) {
  359.         MessageBox (
  360.             GetFocus(),
  361.             "There isn't enough available\nmemory to run Winfract..",
  362.             "Fractint For Windows",
  363.             MB_ICONSTOP | MB_OK);
  364.         return(FALSE);
  365.           }
  366.     pLogPal = (LPLOGPALETTE)GlobalLock(hLogPal); 
  367.     pLogPal->palVersion    = 0x300;
  368.     pLogPal->palNumEntries = PALETTESIZE;
  369.     /* fill in intensities for all palette entry colors */
  370.     for (iLoop = 0; iLoop < PALETTESIZE; iLoop++) {
  371.         pLogPal->palPalEntry[iLoop].peRed   = iLoop;
  372.         pLogPal->palPalEntry[iLoop].peGreen = 0;
  373.         pLogPal->palPalEntry[iLoop].peBlue  = 0;
  374.         pLogPal->palPalEntry[iLoop].peFlags = PC_EXPLICIT;
  375.         }
  376.     /* flip the ugly red color #1 with the pretty blue color #4 */
  377.     if (iNumColors < 0 || iNumColors > 4) {
  378.         pLogPal->palPalEntry[1].peRed = 4;
  379.         pLogPal->palPalEntry[4].peRed = 1;
  380.         }
  381.     /*  create a logical color palette according the information
  382.         in the LOGPALETTE structure. */
  383.     hPal = CreatePalette ((LPLOGPALETTE) pLogPal) ;
  384.     hDC = GetDC(GetFocus());
  385.     SetMapMode(hDC,MM_TEXT);
  386.     SelectPalette (hDC, hPal, 1);
  387.     RealizePalette(hDC);
  388.     /* get the "real" colors */
  389.     jLoop = iNumColors;
  390.     if (jLoop < 0) jLoop = -1;
  391.     for (iLoop = 0; iLoop < PALETTESIZE; iLoop++){
  392.         if (++jLoop >= iNumColors)
  393.             if (iNumColors > 0)
  394.                 jLoop = 0;
  395.         ThisColor = GetNearestColor (hDC, PALETTEINDEX (jLoop) );
  396.         win_dacbox[iLoop][0] = dacbox[iLoop][0] =
  397.             ((BYTE)GetRValue (ThisColor)) >> 2;
  398.         win_dacbox[iLoop][1] = dacbox[iLoop][1] =
  399.             ((BYTE)GetGValue (ThisColor)) >> 2;
  400.         win_dacbox[iLoop][2] = dacbox[iLoop][2] =
  401.             ((BYTE)GetBValue (ThisColor)) >> 2;
  402.         }
  403.     ReleaseDC(GetFocus(),hDC);
  404.  
  405.     /* allocate a device-independent bitmap header */
  406.     if (!(hDibInfo = GlobalAlloc(GMEM_FIXED,
  407.         sizeof(BITMAPINFOHEADER)+256*sizeof(PALETTEENTRY)))) {
  408.         MessageBox (
  409.             GetFocus(),
  410.             "There isn't enough available\nmemory to run Winfract...",
  411.             "Fractint For Windows",
  412.             MB_ICONSTOP | MB_OK);
  413.         return(FALSE);
  414.         }
  415.     pDibInfo = (LPBITMAPINFO)GlobalLock(hDibInfo);
  416.     /* fill in the header */
  417.     pDibInfo->bmiHeader.biSize  = (long)sizeof(BITMAPINFOHEADER);
  418.     pDibInfo->bmiHeader.biWidth  = win_xdots;
  419.     pDibInfo->bmiHeader.biHeight = win_ydots;
  420.     pDibInfo->bmiHeader.biSizeImage = (DWORD)win_xdots * win_ydots;
  421.     pDibInfo->bmiHeader.biPlanes = 1;
  422.     pDibInfo->bmiHeader.biBitCount = 8;
  423.     pDibInfo->bmiHeader.biCompression = BI_RGB;
  424.     pDibInfo->bmiHeader.biXPelsPerMeter = 0L;
  425.     pDibInfo->bmiHeader.biYPelsPerMeter = 0L;
  426.     pDibInfo->bmiHeader.biClrUsed = 0L;
  427.     pDibInfo->bmiHeader.biClrImportant = 0L;
  428.     default_dib_palette();
  429.  
  430.     IconWidth = GetSystemMetrics(SM_CXICON);
  431.     IconHeight = GetSystemMetrics(SM_CYICON);
  432.     IconSize = (IconWidth * IconHeight) >> pixelshift_per_byte;
  433.     hIconBitmap = GlobalAlloc(GMEM_DISCARDABLE, IconSize);
  434.  
  435.     /* allocate and lock a pixel array for the initial bitmap */
  436.     hpixels = (HANDLE) 0;
  437.     pixels = (char huge *) NULL;
  438.     if (hIconBitmap && clear_screen(0))
  439.         return(TRUE);
  440.  
  441.     MessageBox (
  442.         GetFocus(),
  443.         "There isn't enough available\nmemory to run Winfract....",
  444.         "Fractint For Windows",
  445.          MB_ICONSTOP | MB_OK);
  446.  
  447.     return (FALSE);
  448.  
  449. }
  450.  
  451. /****************************************************************************
  452.  
  453.     FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)
  454.  
  455.     PURPOSE:  Processes messages
  456.  
  457. ****************************************************************************/
  458.  
  459. void lmemcpy(  LPSTR to,  LPSTR from, long len)
  460. {
  461. long i;
  462.  
  463. for (i = 0; i < len; i++)
  464.   to[i] = from[i];
  465. }
  466.  
  467. long FAR PASCAL MainWndProc(hWnd, message, wParam, lParam)
  468. HWND hWnd;                /* handle to main window */
  469. unsigned message;
  470. WORD wParam;
  471. LONG lParam;
  472. {
  473.  
  474.     RECT tempRect;
  475.  
  476.     int IOStatus;                           /* result of file i/o      */
  477.     int Return;
  478.     int i, fchoice;
  479.  
  480.     switch (message) {
  481.  
  482.         case WM_INITMENU:
  483.            if (!iRasterCaps || iNumColors < 16) {
  484.                EnableMenuItem(GetMenu(hWnd), IDM_CYCLE, MF_DISABLED);
  485.                EnableMenuItem(GetMenu(hWnd), IDM_CYCLE, MF_GRAYED);
  486.                }
  487.            return (TRUE);
  488.  
  489.        case WM_KEYDOWN:
  490.            switch (wParam) {
  491.                case VK_F1:
  492.                /* F1, shifted F1 bring up the Help Index */
  493.                    WinHelp(hWnd,szHelpFileName,HELP_INDEX,0L);
  494.                    break;
  495.                
  496.                case VK_ESCAPE:
  497.                   if(Zooming)
  498.                      CancelZoom();
  499.                   break;
  500.                case VK_SPACE:
  501.                /* Space/etc.. toggles Color-cycling parameters */
  502.                    if (time_to_cycle == 1)
  503.                        time_to_cycle = 0;
  504.                    else 
  505.                        if (win_oktocycle())
  506.                             time_to_cycle = 1;
  507.                    break;
  508.                case VK_RIGHT:
  509.                case VK_ADD: 
  510.                    if (win_oktocycle()) {
  511.                        time_to_cycle = 1;
  512.                        win_cycledir = -1;
  513.                        }
  514.                    break;
  515.                case VK_LEFT:
  516.                case VK_SUBTRACT: 
  517.                    if (win_oktocycle()) {
  518.                        time_to_cycle = 1;
  519.                        win_cycledir = 1;
  520.                        }
  521.                    break;
  522.                case VK_RETURN:
  523.                    if(Zooming)
  524.                       ExecuteZoom();
  525.                    else if (win_oktocycle()) {
  526.                        time_to_cycle = 1;
  527.                        win_cyclerand = 2;
  528.                        }
  529.                    break;
  530.                }
  531.            break;
  532.         case WM_LBUTTONDBLCLK:
  533.             if(Zooming)
  534.                ExecuteZoom();
  535.             break;
  536.  
  537.         case WM_LBUTTONDOWN:           /* message: left mouse button pressed */
  538.  
  539.             /* Start selection of region */
  540.  
  541.             if(!Zooming) {
  542.                bTrack = TRUE;
  543.                SetRectEmpty(&Rect);
  544.                StartSelection(hWnd, MAKEPOINT(lParam), &Rect,
  545.                    (wParam & MK_SHIFT) ? (SL_EXTEND | Shape) : Shape);
  546.             }
  547.             else
  548.                StartZoomTracking(lParam);
  549.             break;
  550.  
  551.         case WM_MOUSEMOVE:                        /* message: mouse movement */
  552.  
  553.             /* Update the selection region */
  554.  
  555.             if (bTrack)
  556.                 UpdateSelection(hWnd, MAKEPOINT(lParam), &Rect, Shape);
  557.             if(CoordBoxOpen)
  558.                 UpdateCoordBox(lParam);
  559.             if(TrackingZoom)
  560.                TrackZoom(lParam);
  561.             break;
  562.  
  563.         case WM_LBUTTONUP:            /* message: left mouse button released */
  564.  
  565.             if (bTrack) {
  566.  
  567.                /* End the selection */
  568.  
  569.                EndSelection(MAKEPOINT(lParam), &Rect);
  570.  
  571.                ClearSelection(hWnd, &Rect, Shape);
  572.                
  573.                if (abs(Rect.bottom - Rect.top ) > 5 &&
  574.                    abs(Rect.right  - Rect.left) > 5) {
  575.                    
  576.                    float win_aspect, zoom_aspect, rel_aspect;
  577.  
  578.                    ytop    = Rect.top;
  579.                    ybottom = Rect.bottom;
  580.                    xleft   = Rect.left;
  581.                    xright  = Rect.right;
  582.                    xleft    = xleft   + win_xoffset;
  583.                    ytop     = ytop    + win_yoffset;
  584.                    xright   = xright  + win_xoffset;
  585.                    ybottom  = ybottom + win_yoffset;
  586.                    
  587.                    /* adjust the zoom-box for aspect ratio */
  588.                    win_aspect = (float)ydots/(float)xdots;
  589.                    zoom_aspect = (float)(ybottom-ytop+1)/(float)(xright-xleft+1);
  590.                    rel_aspect = win_aspect/zoom_aspect;
  591.                    
  592.                    if (rel_aspect > 1.0 && rel_aspect < 1.50) {
  593.                        ybottom = ytop + ((xright-xleft+1)*win_aspect) - 1;
  594.                        if (ybottom >= ydots) ybottom = ydots-1;
  595.                        }
  596.                    if (rel_aspect < 1.0 && rel_aspect > 0.66) {
  597.                        xright = xleft + ((ybottom-ytop+1)/win_aspect) - 1;
  598.                        if (xright >= xdots) xright = xdots-1;
  599.                        }
  600.                    
  601.                    zoomflag = TRUE;
  602.                    time_to_restart = 1;
  603.                    time_to_cycle = 0;
  604.                    calc_status = 0;
  605.                    }
  606.  
  607.                bTrack = FALSE;
  608.  
  609.             }
  610.             if(TrackingZoom)
  611.                EndZoom(lParam);
  612.             break;
  613.  
  614.         case WM_RBUTTONUP:
  615.             {
  616.             int xx, yy;
  617.             xx = LOWORD(lParam);
  618.             yy = HIWORD(lParam);
  619.             if (xx >= xdots || yy >= ydots) 
  620.                 break;
  621.             if (fractalspecific[fractype].tojulia != NOFRACTAL
  622.                 && param[0] == 0.0 && param[1] == 0.0) {
  623.                 /* switch to corresponding Julia set */
  624.                 fractype = fractalspecific[fractype].tojulia;
  625.                 param[0] = xxmin + (xxmax - xxmin) * xx / xdots;
  626.                 param[1] = yymax - (yymax - yymin) * yy / ydots;
  627.                 jxxmin = xxmin; jxxmax = xxmax;
  628.                 jyymax = yymax; jyymin = yymin;
  629.                 jxx3rd = xx3rd; jyy3rd = yy3rd;
  630.                 frommandel = 1;
  631.                 xxmin = fractalspecific[fractype].xmin;
  632.                 xxmax = fractalspecific[fractype].xmax;
  633.                 yymin = fractalspecific[fractype].ymin;
  634.                 yymax = fractalspecific[fractype].ymax;
  635.                 xx3rd = xxmin;
  636.                 yy3rd = yymin;
  637.                 if(biomorph != -1 && bitshift != 29) {
  638.                    xxmin *= 3.0;
  639.                    xxmax *= 3.0;
  640.                    yymin *= 3.0;
  641.                    yymax *= 3.0;
  642.                    xx3rd *= 3.0;
  643.                    yy3rd *= 3.0;
  644.                    }
  645.                 calc_status = 0;
  646.                 }
  647.             else if (fractalspecific[fractype].tomandel != NOFRACTAL) {
  648.                 /* switch to corresponding Mandel set */
  649.                 fractype = fractalspecific[fractype].tomandel;
  650.                 if (frommandel) {
  651.                     xxmin = jxxmin;  xxmax = jxxmax;
  652.                     yymin = jyymin;  yymax = jyymax;
  653.                     xx3rd = jxx3rd;  yy3rd = jyy3rd;
  654.                     }
  655.                 else {
  656.                     double ccreal,ccimag;
  657.                     ccreal = (fractalspecific[fractype].xmax - fractalspecific[fractype].xmin) / 2;
  658.                     ccimag = (fractalspecific[fractype].ymax - fractalspecific[fractype].ymin) / 2;
  659.                     xxmin = xx3rd = param[0] - ccreal;
  660.                     xxmax = param[0] + ccreal;
  661.                     yymin = yy3rd = param[1] - ccimag;
  662.                     yymax = param[1] + ccimag;
  663.                     }
  664.                 frommandel = 0;
  665.                 param[0] = 0;
  666.                 param[1] = 0;
  667.                 calc_status = 0;
  668.                 }
  669.             else {
  670.                 buzzer(2); /* can't switch */
  671.                 break;
  672.                 }
  673.  
  674.             ytop    = 0;
  675.             ybottom = ydots-1;
  676.             xleft   = 0;
  677.             xright  = xdots-1;
  678.  
  679.             time_to_restart  = 1;
  680.             time_to_cycle = 0;
  681.             calc_status = 0;
  682.             }
  683.             break;
  684.  
  685.         case WM_CREATE:
  686.  
  687.             /* the scroll bars are hard-coded to 100 possible values */
  688.             xposition = yposition = 0;      /* initial scroll-bar positions */
  689.             SetScrollRange(hWnd,SB_HORZ,0,100,FALSE);
  690.             SetScrollRange(hWnd,SB_VERT,0,100,FALSE);
  691.             SetScrollPos(hWnd,SB_HORZ,xposition,TRUE);
  692.             SetScrollPos(hWnd,SB_VERT,yposition,TRUE);
  693.             InitializeParameters(hWnd);
  694.             break;
  695.  
  696.         case WM_SIZE:
  697.             xpagesize = LOWORD(lParam);        /* remember the window size */
  698.             ypagesize = HIWORD(lParam);
  699.         set_win_offset();
  700.             if(!ReSizing && !IsIconic(hWnd))
  701.            ResizeWindow(hWnd);
  702.             break;
  703.  
  704.         case WM_MOVE:
  705.             SaveWindowPosition(hWnd, WinfractPosStr);
  706.             break;
  707.  
  708.         case WM_HSCROLL:
  709.             switch (wParam) {
  710.                case SB_LINEDOWN:       xposition += 1; break;
  711.                case SB_LINEUP:         xposition -= 1; break;
  712.                case SB_PAGEDOWN:       xposition += 10; break;
  713.                case SB_PAGEUP:         xposition -= 10; break;
  714.                case SB_THUMBPOSITION:  xposition = LOWORD(lParam);
  715.                default:                break;
  716.                }
  717.             if (xposition > 100) xposition = 100;
  718.             if (xposition <     0) xposition = 0;
  719.             if (xposition != GetScrollPos(hWnd,SB_HORZ)) {
  720.                SetScrollPos(hWnd,SB_HORZ,xposition,TRUE);
  721.                InvalidateRect(hWnd,NULL,TRUE);
  722.                }
  723.             set_win_offset();
  724.            break;
  725.         case WM_VSCROLL:
  726.             switch (wParam) {
  727.                case SB_LINEDOWN:       yposition += 1; break;
  728.                case SB_LINEUP:         yposition -= 1; break;
  729.                case SB_PAGEDOWN:       yposition += 10; break;
  730.                case SB_PAGEUP:         yposition -= 10; break;
  731.                case SB_THUMBPOSITION:  yposition = LOWORD(lParam);
  732.                default:                break;
  733.                }
  734.             if (yposition > 100) yposition = 100;
  735.             if (yposition <     0) yposition = 0;
  736.             if (yposition != GetScrollPos(hWnd,SB_VERT)) {
  737.                SetScrollPos(hWnd,SB_VERT,yposition,TRUE);
  738.                InvalidateRect(hWnd,NULL,TRUE);
  739.                }
  740.             set_win_offset();
  741.             break;
  742.         case WM_CLOSE:
  743.             goto GlobalExit;
  744.  
  745.         case WM_COMMAND:
  746.             switch (wParam) {
  747.                 case IDM_COORD:
  748.                     CoordinateBox(hWnd);
  749.                     break;
  750.                 case IDM_ABOUT:
  751.                     lpProcAbout = MakeProcInstance(About, hInst);
  752.                     DialogBox(hInst, "AboutBox", hWnd, lpProcAbout);
  753.                     FreeProcInstance(lpProcAbout);
  754.                     break;
  755.  
  756.                case IDM_HELP_INDEX:
  757.                    WinHelp(hWnd,szHelpFileName,HELP_INDEX,0L);
  758.                    break;
  759.  
  760.                case IDM_HELP_KEYBOARD:
  761.            WinHelp(hWnd,szHelpFileName,HELP_KEY,(DWORD)(LPSTR)"keys");
  762.                    break;
  763.  
  764.                case IDM_HELP_HELP:
  765.            WinHelp(hWnd,"WINHELP.HLP",HELP_INDEX,0L);
  766.                    break;
  767.                    
  768.                 case IDM_COPY:
  769.                    /* allocate the memory for the BITMAPINFO structure 
  770.                       (followed by the bitmap bits) */
  771.                    if (!(hClipboard1 = GlobalAlloc(GMEM_FIXED,
  772.                    sizeof(BITMAPINFOHEADER)+colors*sizeof(PALETTEENTRY)
  773.                    + win_bitmapsize))) {
  774.                         cant_clip();
  775.                         return(TRUE);
  776.                         }
  777.                     if (!(lpClipboard1 = 
  778.                         (LPSTR) GlobalLock(hClipboard1))) {
  779.                         GlobalFree(hClipboard1);
  780.                         cant_clip();
  781.                         return(TRUE);
  782.                         }
  783.                     rgb_dib_palette();
  784.                     lmemcpy((LPSTR)lpClipboard1, (LPSTR)pDibInfo,
  785.                         sizeof(BITMAPINFOHEADER)+colors*sizeof(RGBQUAD)
  786.                         );
  787.                     lpClipboard1 += 
  788.                         (sizeof(BITMAPINFOHEADER))+
  789.                         (colors*sizeof(RGBQUAD));
  790.                     lmemcpy((LPSTR)lpClipboard1, (LPSTR)pixels,
  791.                         win_bitmapsize);
  792.                     GlobalUnlock(hClipboard1);
  793.  
  794.                    /* allocate the memory for the palette info */
  795.                    if (!lpClipboard2) {
  796.                        if (!(hClipboard2 = GlobalAlloc (GMEM_FIXED,
  797.                            (sizeof (LOGPALETTE) +
  798.                            (sizeof (PALETTEENTRY) * (PALETTESIZE)))))) {
  799.                             GlobalFree(hClipboard1);
  800.                             cant_clip();
  801.                             return(TRUE);
  802.                             }
  803.                         if (!(lpClipboard2 = 
  804.                             (LPSTR) GlobalLock(hClipboard2))) {
  805.                             GlobalFree(hClipboard1);
  806.                             GlobalFree(hClipboard2);
  807.                             cant_clip();
  808.                             return(TRUE);
  809.                             }
  810.                        }
  811.                    /* fill in the palette info */
  812.                    lpClipboard2[0] = 0;
  813.                    lpClipboard2[1] = 3;
  814.                    lpClipboard2[2] = 0;
  815.                    lpClipboard2[3] = 1;
  816.                    lmemcpy((LPSTR)&lpClipboard2[4],
  817.                         (LPSTR)&pDibInfo->bmiColors[0],
  818.                         PALETTESIZE*sizeof(RGBQUAD)
  819.                         );
  820.                    hClipboard3 = CreatePalette ((LPLOGPALETTE) lpClipboard2);
  821.  
  822.                     if (OpenClipboard(hWnd)) {
  823.                         EmptyClipboard();
  824.                         SetClipboardData(CF_PALETTE, hClipboard3);
  825.                         SetClipboardData(CF_DIB, hClipboard1);
  826.                         CloseClipboard();
  827.                         }
  828.  
  829.                     default_dib_palette();
  830.  
  831.                     break;
  832.  
  833.                 case IDM_NEW:
  834.                 case IDM_OPEN:
  835.                 case IDM_3D:
  836.                 case IDM_3DOVER:
  837.                     win_display3d = 0;
  838.                     win_overlay3d = 0;
  839.                     /* Call OpenDlg() to get the filename */
  840.                     strcpy(DialogTitle,"Select a File to Open");
  841.                     strcpy(FileName, readname);
  842.                     strcpy(DefSpec,"*.gif");
  843.                     strcpy(DefExt,".gif");
  844.                     lpOpenDlg = MakeProcInstance((FARPROC) OpenDlg, hInst);
  845.                     Return = DialogBox(hInst, "Open", hWnd, lpOpenDlg);
  846.                     FreeProcInstance(lpOpenDlg);
  847.                     if (Return && (wParam == IDM_3D || wParam == IDM_3DOVER)) {
  848.                         lpSelect3D = MakeProcInstance(
  849.                             (FARPROC) Select3D, hInst);
  850.                         Return = DialogBox(hInst, "Select3D",
  851.                              hWnd, lpSelect3D);
  852.                         FreeProcInstance(lpSelect3D);
  853.                         if (Return && !win_3dspherical) {
  854.                             lpSelect3DPlanar = MakeProcInstance(
  855.                                 (FARPROC) Select3DPlanar, hInst);
  856.                             Return = DialogBox(hInst, "Select3DPlanar",
  857.                                  hWnd, lpSelect3DPlanar);
  858.                             FreeProcInstance(lpSelect3DPlanar);
  859.                             }
  860.                         if (Return && win_3dspherical) {
  861.                             lpSelect3DSpherical = MakeProcInstance(
  862.                                 (FARPROC) Select3DSpherical, hInst);
  863.                             Return = DialogBox(hInst, "Select3DSpherical",
  864.                                  hWnd, lpSelect3DSpherical);
  865.                             FreeProcInstance(lpSelect3DSpherical);
  866.                             }
  867.                         }
  868.                     if (Return) {
  869.                         strcpy(readname,FileName);
  870.                         time_to_load = 1;
  871.                         time_to_cycle = 0;
  872.                         if (wParam == IDM_3D || wParam == IDM_3DOVER)
  873.                             win_display3d = 1;
  874.                         if (wParam == IDM_3DOVER)
  875.                             win_overlay3d = 1;
  876.                         }
  877.                     break;
  878.  
  879.                 case IDM_MAPIN:
  880.                 case IDM_MAPOUT:
  881.                     if (!win_oktocycle())
  882.                         break;
  883.                     /* Call OpenDlg() to get the filename */
  884.                     strcpy(DialogTitle,"Select a File to Open");
  885.                     strcpy(FileName, "default");
  886.                     if (wParam == IDM_MAPOUT)
  887.                         strcpy(FileName, "mymap");
  888.                     strcpy(DefSpec,"*.map");
  889.                     strcpy(DefExt,".map");
  890.                     lpOpenDlg = MakeProcInstance((FARPROC) OpenDlg, hInst);
  891.                     Return = DialogBox(hInst, "Open", hWnd, lpOpenDlg);
  892.                     FreeProcInstance(lpOpenDlg);
  893.                     if (Return && wParam == IDM_MAPIN) {
  894.                         ValidateLuts(FileName);
  895.                         spindac(0,1);
  896.                         }
  897.                     if (Return && wParam == IDM_MAPOUT) {
  898.                         FILE *dacfile;
  899.             dacfile = fopen(FileName,"w");
  900.             if (dacfile == NULL) {
  901.                 break;
  902.                 }
  903.             fprintf(dacfile,"  0   0   0\n");
  904.             for (i = 1; i < 256; i++)
  905.                 fprintf(dacfile, "%3d %3d %3d\n",
  906.                 dacbox[i][0] << 2,
  907.                 dacbox[i][1] << 2,
  908.                 dacbox[i][2] << 2);
  909.             fclose(dacfile);
  910.                         }
  911.                     break;
  912.  
  913.                 case IDM_SAVE:
  914.                 case IDM_SAVEAS:
  915.                     /* Call the SaveAsDlg() function to get the new filename */
  916.                     strcpy(DialogTitle,"Select a File to Save");
  917.                     strcpy(FileName, readname);
  918.                     strcpy(DefSpec,"*.gif");
  919.                     strcpy(DefExt,".gif");
  920.                     lpSaveAsDlg = MakeProcInstance(SaveAsDlg, hInst);
  921.                     Return = DialogBox(hInst, "SaveAs", hWnd, lpSaveAsDlg);
  922.                     FreeProcInstance(lpSaveAsDlg);
  923.                     Return = time_to_save; /* ?? ugly klooge */
  924.                     if (Return) {
  925.                         strcpy(readname,FileName);
  926.                         time_to_save = 1;
  927.                         time_to_cycle = 0;
  928.                         }
  929.                     break;                                  /* User canceled */
  930.  
  931.         case IDM_SIZING:
  932.             WindowSizing(hWnd);
  933.             break;
  934.  
  935.                 case IDM_PRINT:
  936.                     time_to_print = 1;
  937.                     time_to_cycle = 0;
  938.                     break;
  939.  
  940.                 case IDM_FORMULA:
  941.                     strcpy(DialogTitle,"Select a Fractal Formula");
  942.                     win_numchoices = CountFractalList;
  943.                     win_choicemade = 0;
  944.                     CurrentFractal = fractype;
  945.                     for (i = 0; i < win_numchoices; i++) {
  946.                         win_choices[i] = fractalspecific[onthelist[i]].name;
  947.                         if (onthelist[i] == fractype ||
  948.                             fractalspecific[onthelist[i]].tofloat == fractype)
  949.                             win_choicemade = i;
  950.                         }
  951.                     lpSelectFractal = MakeProcInstance(SelectFractal, hInst);
  952.                     Return = DialogBox(hInst, "SelectFractal",
  953.                         hWnd, lpSelectFractal);
  954.                     FreeProcInstance(lpSelectFractal);
  955.                     fchoice = win_choicemade;
  956.                     if (Return && (onthelist[fchoice] == IFS ||
  957.                         onthelist[fchoice] == IFS3D)) {
  958.                         strcpy(DialogTitle,"Select an IFS Filename");
  959.                         strcpy(FileName, IFSFileName);
  960.                         strcpy(DefSpec,"*.ifs");
  961.                         strcpy(DefExt,".ifs");
  962.                         lpOpenDlg = MakeProcInstance((FARPROC) OpenDlg, hInst);
  963.                         Return = DialogBox(hInst, "Open", hWnd, lpOpenDlg);
  964.                         FreeProcInstance(lpOpenDlg);
  965.                         if (Return) {
  966.                             strcpy(IFSFileName, FileName);
  967.                             strcpy(FormFileName, FileName);
  968.                             get_formula_names();
  969.                             strcpy(DialogTitle,"Select an IFS type");
  970.                             win_choicemade = 0;
  971.                             lpSelectFractal = MakeProcInstance(SelectFractal, hInst);
  972.                             Return = DialogBox(hInst, "SelectFractal",
  973.                                 hWnd, lpSelectFractal);
  974.                             FreeProcInstance(lpSelectFractal);
  975.                             if (Return) {
  976.                                 strcpy(IFSName, win_choices[win_choicemade]);
  977.                                 Return = ! ifsload();
  978.                                 }
  979.                             }
  980.                         }
  981.                     if (Return && (onthelist[fchoice] == FORMULA || 
  982.                         onthelist[fchoice] == FFORMULA)) {
  983.                         /* obtain the formula filename */
  984.                         strcpy(DialogTitle,"Select a Formula File");
  985.                         strcpy(FileName, FormFileName);
  986.                         strcpy(DefSpec,"*.frm");
  987.                         strcpy(DefExt,".frm");
  988.                         lpOpenDlg = MakeProcInstance((FARPROC) OpenDlg, hInst);
  989.                         Return = DialogBox(hInst, "Open", hWnd, lpOpenDlg);
  990.                         FreeProcInstance(lpOpenDlg);
  991.                         if (Return) {
  992.                             strcpy(FormFileName, FileName);
  993.                             get_formula_names();
  994.                             strcpy(DialogTitle,"Select a Formula");
  995.                             win_choicemade = 0;
  996.                             lpSelectFractal = MakeProcInstance(SelectFractal, hInst);
  997.                             Return = DialogBox(hInst, "SelectFractal",
  998.                                 hWnd, lpSelectFractal);
  999.                             FreeProcInstance(lpSelectFractal);
  1000.                             if (Return) 
  1001.                                 Return = parse_formula_names();
  1002.                             }
  1003.                         }
  1004.                     if (Return && (onthelist[fchoice] == LSYSTEM)) {
  1005.                         /* obtain the lsystem formula filename */
  1006.                         strcpy(DialogTitle,"Select an Lsystem File");
  1007.                         strcpy(FileName, LFileName);
  1008.                         strcpy(DefSpec,"*.l");
  1009.                         strcpy(DefExt,".l");
  1010.                         lpOpenDlg = MakeProcInstance((FARPROC) OpenDlg, hInst);
  1011.                         Return = DialogBox(hInst, "Open", hWnd, lpOpenDlg);
  1012.                         FreeProcInstance(lpOpenDlg);
  1013.                         if (Return) {
  1014.                             strcpy(LFileName, FileName);
  1015.                             get_lsys_name();
  1016.                             strcpy(DialogTitle,"Select a Formula");
  1017.                             win_choicemade = 0;
  1018.                             lpSelectFractal = MakeProcInstance(SelectFractal, hInst);
  1019.                             Return = DialogBox(hInst, "SelectFractal",
  1020.                                 hWnd, lpSelectFractal);
  1021.                             FreeProcInstance(lpSelectFractal);
  1022.                             if (Return) {
  1023.                                 strcpy(LName, win_choices[win_choicemade]);
  1024.                                 Return = !LLoad();
  1025.                                 }
  1026.                             }
  1027.                         }
  1028.                     if (Return) {
  1029.                         CurrentFractal = onthelist[fchoice];
  1030.                         lpSelectFracParams = MakeProcInstance(SelectFracParams,
  1031.                             hInst);
  1032.                         Return = DialogBox(hInst, "SelectFracParams",
  1033.                             hWnd, lpSelectFracParams);
  1034.                         FreeProcInstance(lpSelectFracParams);
  1035.                         }
  1036.                     if (Return) {
  1037.             restoredac();
  1038.                         time_to_reinit = 1;
  1039.                         time_to_cycle = 0;
  1040.                         calc_status = 0;
  1041.                         }
  1042.                     break;
  1043.  
  1044.                 case IDM_IMAGE:
  1045.             lpSelectImage = MakeProcInstance(SelectImage, hInst);
  1046.             Return = DialogBox(hInst, "SelectImage",
  1047.                 hWnd, lpSelectImage);
  1048.             FreeProcInstance(lpSelectImage);
  1049.             if (Return) {
  1050.                 restoredac();
  1051.                 time_to_restart = 1;
  1052.                                 time_to_cycle = 0;
  1053.                                 calc_status = 0;
  1054.                 }
  1055.             ResizeWindow(hWnd);
  1056.                         break;
  1057.  
  1058.                 case IDM_DOODADS:
  1059.             lpSelectDoodads = MakeProcInstance(SelectDoodads, hInst);
  1060.             Return = DialogBox(hInst, "SelectDoodads",
  1061.                 hWnd, lpSelectDoodads);
  1062.             FreeProcInstance(lpSelectDoodads);
  1063.             if (Return) {
  1064.                 restoredac();
  1065.                 time_to_restart = 1;
  1066.                                 time_to_cycle = 0;
  1067.                                 calc_status = 0;
  1068.                 }
  1069.                         break;
  1070.  
  1071.                 case IDM_CYCLE:
  1072.                     if (!win_oktocycle())
  1073.                         break;
  1074.                     lpSelectCycle = MakeProcInstance(SelectCycle, hInst);
  1075.                     Return = DialogBox(hInst, "SelectCycle",
  1076.                     hWnd, lpSelectCycle);
  1077.                     FreeProcInstance(lpSelectCycle);
  1078.                     break;
  1079.  
  1080.                 case IDM_MATH_TOOLS:
  1081.                     MathToolBox(hWnd);
  1082.                     break;
  1083.  
  1084.                 case IDM_ZOOM:
  1085.                     ZoomBar(hWnd);
  1086.                     break;
  1087.  
  1088.                 case IDS_STATUS:
  1089.                     lpProcStatus = MakeProcInstance(Status, hInst);
  1090.                     DialogBox(hInst, "ShowStatus", hWnd, lpProcStatus);
  1091.                     FreeProcInstance(lpProcStatus);
  1092.                     break;
  1093.  
  1094.                 GlobalExit:
  1095.                 case IDM_EXIT:
  1096.                     time_to_quit = 1;
  1097.                     time_to_cycle = 0;
  1098.                     ValidateRect(hWnd, NULL);
  1099.                     hWndCopy = hWnd;
  1100.                     /* the main routine will actually call 'DestroyWindow()' */
  1101.                     break;
  1102.  
  1103.                 case IDC_EDIT:
  1104.                     if (HIWORD (lParam) == EN_ERRSPACE) {
  1105.                         MessageBox (
  1106.                               GetFocus ()
  1107.                             , "Out of memory."
  1108.                             , "Fractint For Windows"
  1109.                             , MB_ICONSTOP | MB_OK
  1110.                         );
  1111.                     }
  1112.                     break;
  1113.  
  1114.             } 
  1115.             break;
  1116.  
  1117.         case WM_PAINT:
  1118.             if (screen_to_be_cleared && last_written_y < 0) {
  1119.                  /* an empty window */
  1120.                  screen_to_be_cleared = 0;
  1121.                  GetUpdateRect(hWnd, &tempRect, TRUE);
  1122.                  hDC = BeginPaint(hWnd,&ps);
  1123.                  if (last_written_y == -2)
  1124.                      last_written_y = -1;
  1125.                  else
  1126.                      BitBlt(hDC, 0, 0, xdots, ydots,
  1127.                          NULL, 0, 0, BLACKNESS);
  1128.                  ValidateRect(hWnd, &tempRect);
  1129.                  EndPaint(hWnd,&ps);
  1130.                  break;
  1131.                  }
  1132.             if(IsIconic(hWnd)) {
  1133.                int x, y;
  1134.                LPSTR icon;
  1135.                long lx, ly, dlx, dly;
  1136.                DWORD tSize, tWidth, tHeight;
  1137.  
  1138.                if((icon = GlobalLock(hIconBitmap)) == NULL)
  1139.                   break;
  1140.  
  1141.                dlx = ((long)xdots << 8) / IconWidth;
  1142.                dly = ((long)ydots << 8) / IconHeight;
  1143.                for(ly = y = 0; y < IconHeight; y++, ly += dly)
  1144.                {
  1145.                   for(lx = x = 0; x < IconWidth; x++, lx += dlx)
  1146.                   {
  1147.                      unsigned long i;
  1148.                      unsigned j, ix, iy;
  1149.                      unsigned char color;
  1150.  
  1151.                      ix = lx >> 8;
  1152.                      iy = ly >> 8;
  1153.                      color = getcolor(ix, iy);
  1154.  
  1155.                      ix = IconWidth - y - 1;
  1156.                      ix = (ix * IconWidth) + x;
  1157.                      iy = ix & pixels_per_bytem1;
  1158.                      ix = ix >> pixelshift_per_byte;
  1159.                      icon[ix] = (icon[ix] & win_notmask[iy]) +
  1160.                                (color << win_bitshift[iy]);
  1161.                   }
  1162.                }
  1163.  
  1164.                hDC = BeginPaint(hWnd, &ps);
  1165.  
  1166.                SelectPalette (hDC, hPal, 0);
  1167.                RealizePalette(hDC);
  1168.  
  1169.                tSize   = pDibInfo->bmiHeader.biSizeImage;
  1170.                tHeight = pDibInfo->bmiHeader.biHeight;
  1171.                tWidth  = pDibInfo->bmiHeader.biWidth;
  1172.  
  1173.                pDibInfo->bmiHeader.biSizeImage = IconSize;
  1174.                pDibInfo->bmiHeader.biHeight    = IconHeight;
  1175.                pDibInfo->bmiHeader.biWidth     = IconWidth;
  1176.  
  1177.                SetDIBitsToDevice(hDC,
  1178.                        2, 2,
  1179.                        IconWidth, IconHeight,
  1180.                        0, 0,
  1181.                        0, IconHeight,
  1182.                        icon, (LPBITMAPINFO)pDibInfo,
  1183.                        DIB_PAL_COLORS);
  1184.  
  1185.                pDibInfo->bmiHeader.biSizeImage = tSize;
  1186.                pDibInfo->bmiHeader.biHeight    = tHeight;
  1187.                pDibInfo->bmiHeader.biWidth     = tWidth;
  1188.  
  1189.                GlobalUnlock(hIconBitmap);
  1190.  
  1191.                EndPaint(hWnd, &ps);
  1192.                break;
  1193.             }
  1194.             screen_to_be_cleared = 0;
  1195.             GetUpdateRect(hWnd, &tempRect, FALSE);
  1196.             PaintMathTools();
  1197.             hDC = BeginPaint(hWnd,&ps);
  1198.             if (last_written_y >= 0) {
  1199.               int i, j, top, bottom, left, right, xcount, ycount;
  1200.               /* bit-blit the invalidated bitmap area */
  1201.               int fromleft, fromtop, fromright, frombottom;
  1202.               long firstpixel;
  1203.               top    = tempRect.top;
  1204.               bottom = tempRect.bottom;
  1205.               left   = tempRect.left;
  1206.               right  = tempRect.right;
  1207.               if (bottom >  ydots) bottom = ydots;
  1208.               if (right  >= xdots) right  = xdots-1;
  1209.               if (top    >  ydots) top    = ydots;
  1210.               if (left   >= xdots) left   = xdots;
  1211.               fromleft  = left  + win_xoffset;
  1212.               fromright = right + win_xoffset;
  1213.               fromtop    = top    + win_yoffset;
  1214.               frombottom = bottom + win_yoffset;
  1215.               xcount = fromright - fromleft + 1;
  1216.               ycount = frombottom - fromtop;
  1217.               firstpixel = win_ydots - frombottom;
  1218.               firstpixel = firstpixel * bytes_per_pixelline;
  1219.               if (left < xdots && top < ydots) {
  1220.                   SelectPalette (hDC, hPal, 0);
  1221.                   RealizePalette(hDC);
  1222.                   SetMapMode(hDC,MM_TEXT);
  1223.                   SetDIBitsToDevice(hDC,
  1224.                        left, top,
  1225.                        xcount, ycount,
  1226.                        fromleft, 0,
  1227.                        0, ydots,
  1228.                        &pixels[firstpixel], (LPBITMAPINFO)pDibInfo,
  1229.                        DIB_PAL_COLORS);
  1230.                   }
  1231.               }
  1232.             ValidateRect(hWnd, &tempRect);
  1233.             EndPaint(hWnd,&ps);
  1234.             PaintMathTools();
  1235.             break;
  1236.  
  1237.         case WM_DESTROY:
  1238.             /* delete the handle to the logical palette if it has any
  1239.                color entries and quit. */
  1240.             if (pLogPal->palNumEntries)
  1241.             DeleteObject (hPal);
  1242.             time_to_quit = 1;
  1243.             time_to_cycle = 0;
  1244.             WinHelp(hWnd,szHelpFileName,HELP_QUIT,0L);
  1245.             if (win_systempaletteused)
  1246.                 win_stop_cycling();
  1247.             SaveParameters(hWnd);
  1248.             GlobalFree(hIconBitmap);
  1249.             PostQuitMessage(0);
  1250.             break;
  1251.  
  1252.         case WM_ACTIVATE:
  1253.             if (!wParam) {  /* app. is being de-activated */
  1254.                 if (win_systempaletteused)
  1255.                     win_stop_cycling();
  1256.                 break;
  1257.                 }
  1258.         case WM_QUERYNEWPALETTE:
  1259.             /* If palette realization causes a palette change,
  1260.              * we need to do a full redraw.
  1261.              */
  1262.              if (last_written_y >= 0) {
  1263.                  hDC = GetDC (hWnd);
  1264.                  SelectPalette (hDC, hPal, 0);
  1265.                  i = RealizePalette(hDC);
  1266.                  ReleaseDC (hWnd, hDC);
  1267.                  if (i) {
  1268.                      InvalidateRect (hWnd, (LPRECT) (NULL), 1);
  1269.                      return 1;
  1270.                      }
  1271.                  else
  1272.                      return FALSE;
  1273.                  }
  1274.              else
  1275.                  return FALSE;
  1276.              break;
  1277.  
  1278.         case WM_PALETTECHANGED:
  1279.             if (wParam != hWnd){
  1280.                 if (last_written_y >= 0) {
  1281.                     hDC = GetDC (hWnd);
  1282.                     SelectPalette (hDC, hPal, 0);
  1283.                     i = RealizePalette (hDC);
  1284.                     if (i)
  1285.                         UpdateColors (hDC);
  1286.                     else
  1287.                         InvalidateRect (hWnd, (LPRECT) (NULL), 1);
  1288.                     ReleaseDC (hWnd, hDC);
  1289.                     }
  1290.                 }
  1291.             break;
  1292.  
  1293.         default:
  1294.             return (DefWindowProc(hWnd, message, wParam, lParam));
  1295.     }
  1296.     return (NULL);
  1297. }
  1298.  
  1299. win_oktocycle()
  1300. {
  1301. if (!(iRasterCaps) || iNumColors < 16) {
  1302.     stopmsg(0,
  1303.         "I'm sorry, but color-cycling and\ncolor maps require a palette-based\nvideo driver."
  1304.          );
  1305.     return(0);
  1306.     }
  1307. return(1);
  1308. }
  1309.  
  1310. extern int win_animate_flag;
  1311.  
  1312. win_stop_cycling()
  1313. {
  1314. HDC hDC;                      /* handle to device context           */
  1315.  
  1316. hDC = GetDC(GetFocus());
  1317. SetSystemPaletteUse(hDC,SYSPAL_STATIC);
  1318. ReleaseDC(GetFocus(),hDC);
  1319.  
  1320. time_to_cycle = 0;
  1321. win_animate_flag = 0;
  1322. restoredac();
  1323. win_systempaletteused = FALSE;
  1324. SetSysColors(COLOR_ENDCOLORS,win_syscolorindex,win_syscolorold);
  1325. }
  1326.  
  1327. mono_dib_palette()
  1328. {
  1329. int i, j, k;        /* fill in the palette index values */
  1330.     for (i = 0; i < 128; i = i+2) {
  1331.         pDibInfo->bmiColors[i  ].rgbBlue      =   0;
  1332.         pDibInfo->bmiColors[i  ].rgbGreen     =   0;
  1333.         pDibInfo->bmiColors[i  ].rgbRed       =   0;
  1334.         pDibInfo->bmiColors[i  ].rgbReserved  =   0;
  1335.         pDibInfo->bmiColors[i+1].rgbBlue      = 255;
  1336.         pDibInfo->bmiColors[i+1].rgbGreen     = 255;
  1337.         pDibInfo->bmiColors[i+1].rgbRed       = 255;
  1338.         pDibInfo->bmiColors[i+1].rgbReserved  =   0;
  1339.         }
  1340. }
  1341.  
  1342. default_dib_palette()
  1343. {
  1344. int i, j, k;        /* fill in the palette index values */
  1345. int far *palette_values;    /* pointer to palette values */
  1346.  
  1347.     palette_values = (int far *)&pDibInfo->bmiColors[0];
  1348.     k = 0;
  1349.     for (i = 0; i < 256; i++) {
  1350.         palette_values[i] = k;
  1351.         if (++k >= iNumColors)
  1352.            if (iNumColors > 0)
  1353.                k = 0;
  1354.         }
  1355. }
  1356.  
  1357. rgb_dib_palette()
  1358. {
  1359. int i;        /* fill in the palette index values */
  1360.  
  1361.     for (i = 0; i < 256; i++) {
  1362.         pDibInfo->bmiColors[i].rgbRed       = dacbox[i][0] << 2;
  1363.         pDibInfo->bmiColors[i].rgbGreen     = dacbox[i][1] << 2;
  1364.         pDibInfo->bmiColors[i].rgbBlue      = dacbox[i][2] << 2;
  1365.         pDibInfo->bmiColors[i].rgbReserved  =   0;
  1366.         }
  1367. }
  1368.  
  1369.  
  1370. /****************************************************************************
  1371.  
  1372.    FUNCTION:   MakeHelpPathName
  1373.  
  1374.    PURPOSE:    Winfract assumes that the .HLP help file is in the same
  1375.                directory as the Winfract executable.This function derives
  1376.                the full path name of the help file from the path of the
  1377.                executable.
  1378.  
  1379. ****************************************************************************/
  1380.  
  1381. void MakeHelpPathName(szFileName)
  1382. char * szFileName;
  1383. {
  1384.    char *  pcFileName;
  1385.    int     nFileNameLen;
  1386.  
  1387.    nFileNameLen = GetModuleFileName(hInst,szFileName,EXE_NAME_MAX_SIZE);
  1388.    pcFileName = szFileName + nFileNameLen;
  1389.  
  1390.    while (pcFileName > szFileName) {
  1391.        if (*pcFileName == '\\' || *pcFileName == ':') {
  1392.            *(++pcFileName) = '\0';
  1393.            break;
  1394.        }
  1395.    nFileNameLen--;
  1396.    pcFileName--;
  1397.    }
  1398.  
  1399.    if ((nFileNameLen+13) < EXE_NAME_MAX_SIZE) {
  1400.        lstrcat(szFileName, "winfract.hlp");
  1401.    }
  1402.  
  1403.    else {
  1404.        lstrcat(szFileName, "?");
  1405.    }
  1406.  
  1407.    return;
  1408. }
  1409.  
  1410. set_win_offset()
  1411. {
  1412. win_xoffset = ((long)xposition*((long)xdots-(long)xpagesize))/100L;
  1413. win_yoffset = ((long)yposition*((long)ydots-(long)ypagesize))/100L;
  1414. if (win_xoffset+xpagesize > xdots) win_xoffset = xdots-xpagesize;
  1415. if (win_yoffset+ypagesize > ydots) win_yoffset = ydots-ypagesize;
  1416. if (xpagesize >= xdots) win_xoffset = 0;
  1417. if (ypagesize >= ydots) win_yoffset = 0;
  1418. }
  1419.  
  1420.  
  1421. /*
  1422.   Read a formula file, picking off the formula names.
  1423.   Formulas use the format "  name = { ... }  name = { ... } "
  1424. */
  1425.  
  1426. int get_formula_names()     /* get the fractal formula names */
  1427. {
  1428.    int numformulas, i;
  1429.    FILE *File;
  1430.    char msg[81], tempstring[201];
  1431.  
  1432.    FormName[0] = 0;        /* start by declaring failure */
  1433.    for (i = 0; i < MaxFormNameChoices; i++) {
  1434.       FormNameChoices[i][0] = 0;
  1435.       win_choices[i] = FormNameChoices[i];
  1436.       }
  1437.  
  1438.    if((File = fopen(FormFileName, "rt")) == NULL) {
  1439.       sprintf("I Can't find %s", FormFileName);
  1440.       stopmsg(1,msg);
  1441.       return(-1);
  1442.    }
  1443.  
  1444.    numformulas = 0;
  1445.    while(fscanf(File, " %20[^ \n\t({]", FormNameChoices[numformulas]) != EOF) {
  1446.       int c;
  1447.  
  1448.       while(c = getc(File)) {
  1449.      if(c == EOF || c == '{' || c == '\n')
  1450.         break;
  1451.       }
  1452.       if(c == EOF)
  1453.      break;
  1454.       else if(c != '\n'){
  1455.      numformulas++;
  1456.      if (numformulas >= MaxFormNameChoices) break;
  1457. skipcomments:
  1458.      if(fscanf(File, "%200[^}]", tempstring) == EOF) break;
  1459.      if (getc(File) != '}') goto skipcomments;
  1460.      if (stricmp(FormNameChoices[numformulas-1],"") == 0 ||
  1461.          stricmp(FormNameChoices[numformulas-1],"comment") == 0)
  1462.          numformulas--;
  1463.       }
  1464.    }
  1465.    fclose(File);
  1466.    win_numchoices = numformulas;
  1467.    qsort(FormNameChoices,win_numchoices,21,strcmp);
  1468.    return(0);
  1469. }
  1470.  
  1471. int parse_formula_names()     /* parse a fractal formula name */
  1472. {
  1473.  
  1474.    strcpy(FormName, win_choices[win_choicemade]);
  1475.  
  1476.    if (RunForm(FormName)) {
  1477.        FormName[0] = 0;     /* declare failure */
  1478.        stopmsg(0,"Can't Parse that Formula");
  1479.        return(0);
  1480.        }
  1481.  
  1482. return(1);
  1483. }
  1484.  
  1485. /* --------------------------------------------------------------------- */
  1486.  
  1487. get_lsys_name()        /* get the Lsystem formula name */
  1488. {
  1489.    int Litem;
  1490.    int numformulas, i;
  1491.    FILE *File;
  1492.    char buf[201];
  1493.  
  1494.    for (i = 0; i < MaxFormNameChoices; i++) {
  1495.       FormNameChoices[i][0] = 0;
  1496.       win_choices[i] = FormNameChoices[i];
  1497.       }
  1498.  
  1499.    if ((File = fopen(LFileName, "rt")) == NULL) {
  1500.       sprintf(buf,"I Can't find %s", LFileName);
  1501.       stopmsg(1,buf);
  1502.       LName[0] = 0;
  1503.       return(-1);
  1504.       }
  1505.  
  1506.    numformulas = 0;
  1507.    while (1) {
  1508.       int c;
  1509.       FormNameChoices[numformulas][0] = 0;
  1510.       if (fscanf(File, " %20[^ \n\t({]", FormNameChoices[numformulas]) == EOF)
  1511.      break;
  1512.       while(c = getc(File)) {
  1513.      if(c == EOF || c == '{' || c == '\n')
  1514.         break;
  1515.      }
  1516.       if(c == EOF)
  1517.      break;
  1518.       else if(c != '\n') {
  1519. skipcomments:
  1520.      if(fscanf(File, "%200[^}]", buf) == EOF) break;
  1521.      if (getc(File) != '}') goto skipcomments;
  1522.      if (stricmp(FormNameChoices[numformulas],"") != 0 &&
  1523.          stricmp(FormNameChoices[numformulas],"comment") != 0)
  1524.          if (++numformulas >= MaxFormNameChoices) break;
  1525.      }
  1526.       }
  1527.    fclose(File);
  1528.  
  1529.    win_numchoices = numformulas;
  1530.    qsort(FormNameChoices,win_numchoices,21,strcmp);
  1531.    return(0);
  1532. }
  1533.  
  1534. cant_clip()
  1535. {
  1536. MessageBox (
  1537.    GetFocus(),
  1538.    "Not Enough Free Memory to Copy to the Clipboard",
  1539.    "Fractint For Windows",
  1540.     MB_ICONSTOP | MB_OK);
  1541.     return(TRUE);
  1542. }
  1543.